﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace View.Web.Controllers
{
    public class LogoController : Controller
    {
        //
        // GET: /Logo/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult New()
        {
            return View();
        }

        public ActionResult Detail(int id)
        {
            using (Model.Business.LogoBusiness logoBL = new Model.Business.LogoBusiness())
            {
                ViewData.Add("DETAIL", logoBL.Select(id));
            }

            return View();
        }

        [HttpPost]
        public ActionResult New (Models.NewLogo data)
        {
            if (ModelState.IsValid)
            {
                Model.Entities.Anuncio anuncio = new Model.Entities.Anuncio();

                try
                {
                    anuncio.Titulo = data.Title;
                    anuncio.SobreEmpresa = data.Company;
                    anuncio.ValorPremiacao = data.Value;
                    anuncio.DetalhesProjeto = data.Briefing;

                    using (Model.Business.LogoBusiness logoBL = new Model.Business.LogoBusiness())
                    {
                        logoBL.Add(anuncio);
                    }

                    return RedirectToAction("Index", "Home");
                }
                catch (Exception e)
                {

                }
                finally
                {
                    anuncio = null;
                    GC.Collect();
                }
            }

            return View(data);
        }

        public ActionResult Requests()
        {
            try
            {
                List<Models.Logo> myRequests = new List<Models.Logo>();
                List<Models.Logo> allRequests = new List<Models.Logo>();
   
                using (Model.Business.LogoBusiness logoBL = new Model.Business.LogoBusiness())
                {
                    if (Framework.Security.User.UserID > 0)
                    {
                        foreach (var item in logoBL.SelectByUser(Framework.Security.User.UserID))
	                    {
                            Models.Logo newLogo = new Models.Logo();
                            newLogo.Title = item.Titulo;
                            newLogo.Company = item.SobreEmpresa;
                            newLogo.Briefing = item.DetalhesProjeto;
                            newLogo.Value = (double)item.ValorPremiacao;
                            if (item.DataEncerramento.HasValue)
                                newLogo.State = "Encerrado";
                            else
                                newLogo.State = "Aguardando";

                            myRequests.Add(newLogo);		 
	                    } 

                        ViewData.Add("MYREQUESTS", myRequests);
                    }

                    foreach (var item in logoBL.Select())
                    {
                        Models.Logo newLogo = new Models.Logo();
                        newLogo.Title = item.Titulo;
                        newLogo.Company = item.SobreEmpresa;
                        newLogo.Briefing = item.DetalhesProjeto;
                        newLogo.Value = (double)item.ValorPremiacao;
                        if (item.DataEncerramento.HasValue)
                            newLogo.State = "Encerrado";
                        else
                            newLogo.State = "Aguardando";

                        allRequests.Add(newLogo);
                    } 

                    ViewData.Add("ALLREQUESTS", allRequests);
                }

                return View();
            }
            catch (Exception e)
            {

            }
            finally
            {
                GC.Collect();
            }

            // get all requests

            return View();
        }

    }
}
